//
//  UserSetViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/1/16.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "UserSetViewController.h"
#import "MoreBlankTableViewHeaderFooterView.h"
//#import "CExpandHeader.h"

#import "UpdateUserDataViewController.h"
#import "UserCollectionViewController.h"

//v3
#import "UserFinanceViewController.h"
#import "UserProjectViewController.h"
#import "UserExtensionViewController.h"
#import "UserSupplyDemandViewController.h"
#import "UserLicensePlateViewController.h"
#import "UserDownloadViewController.h"
#import "UserColleagueViewController.h"
#import "SetViewController.h"
#import "SelectWebUrlViewController.h"
#import "CompanyNoticeViewController.h"

//v4
#import "CRMViewController.h"
#import "UserResumeViewController.h"

//修改
#import "SetPersonalDataVC.h"
#import "NewCRMViewController.h"

#import "V4InputBuyerAndSeller.h"
#import "BuyerAndSellerDetailViewController.h"

@interface UserSetViewController (){
    UIImageView *_imageUserLogo;
    UILabel *_lblUserName;
    UILabel *_lblUserSub;
    UITableView *tableViewUser;
    NSMutableArray *arrayAllData;
//    CExpandHeader *_header;
    
    NSDictionary *dicuserData;
    MKNetworkOperation *opT;
    NSArray *arrayproductsdowncate;
    
    ///v4
    UIButton *btnLeft;
    
}

@end

@implementation UserSetViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault];
    [self refreshUserData];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (opT) {
        [opT cancel];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
      __weak __typeof(self) weakSelf = self;
    
    arrayAllData=[NSMutableArray new];
    RegisterNotify(@"againLoadUserData", @selector(againLoadUserData))
    
                   [self navbarTitle:@"我的"];
    
    
    tableViewUser=[[UITableView alloc]initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, self.view.frameHeight-kTopHeight)];
//    [tableViewUser showRefresh:NO LoadMore:NO];
    tableViewUser.delegate = self;
    tableViewUser.dataSource = self;
    //    tableViewUser.delegate2=self;
    [tableViewUser setBackgroundColor:rgbGray];
    [tableViewUser setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tableViewUser setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [self.view addSubview:tableViewUser];
    [tableViewUser registerClass:[MoreBlankTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"MoreBlankTableViewHeaderFooterView"];
    
    {
        
        UIView*viewHeader=[RHMethods viewWithFrame:CGRectMake(0, 0, kScreenWidth, 88+20) backgroundcolor:nil superView:nil];
        UIView*viewsubHeader=[RHMethods viewWithFrame:CGRectMake(0, 10, kScreenWidth, 88) backgroundcolor:zrgbwhiteColor superView:viewHeader];
        UIImageView*imgVIcon=[RHMethods imageviewWithFrame:CGRectMake(10, 11, 65, 65) defaultimage:@"" supView:viewsubHeader];
        
//        [imgVIcon addViewClickBlock:^(UIView *view) {
//            [weakSelf tapImageuserLogo];
//        }];
        _imageUserLogo=imgVIcon;
        imgVIcon.layer.cornerRadius=5;
        UILabel*lbname=[RHMethods lableX:85 Y:25 W:kScreenWidth-85-5-17-28 Height:17 font:17 superview:viewsubHeader withColor:nil text:@""];
        _lblUserName=lbname;
        UILabel*lbMobile=[RHMethods lableX:lbname.frameX Y:lbname.frameYH+12 W:lbname.frameWidth Height:14 font:14 superview:viewHeader withColor:nil text:@"手机号：15921164016"];
        _lblUserSub=lbMobile;
        UIImageView*imgVQr=[RHMethods imageviewWithFrame:CGRectMake(kScreenWidth-17-28, 0, 17, 17) defaultimage:@"ewmmy" supView:viewsubHeader];
        UIImageView*imgVright=[RHMethods imageviewWithFrame:CGRectMake(kScreenWidth-10-8, 0, 8, 15) defaultimage:@"righticon" supView:viewsubHeader];
        [imgVQr beCY];
        [imgVright beCY];
        
        [tableViewUser setTableHeaderView:viewHeader];
        [viewsubHeader addViewClickBlock:^(UIView *view) {
            [weakSelf rightButtonClicked];
        }];
    
    }
    
        [self againLoadUserData];
}
-(void)againLoadUserData{
    [arrayAllData removeAllObjects];
    if ([[Utility Share] userIsADD]) {
        btnLeft.hidden=YES;
        [arrayAllData addObject:@[
                                  @{@"title":@"重要通知",@"image":@"myicon04"},
                                  @{@"title":@"我的牌照",@"image":@"myicon05"},
                                  @{@"title":@"我的项目",@"image":@"myicon03"},
                                  @{@"title":@"推广效果",@"image":@"myicon04"}]];
        
        [arrayAllData addObject:@[@{@"title":@"合同模板",@"image":@"myicon06"}]];
    }else{
        btnLeft.hidden=NO;
        //
        [arrayAllData addObject:@[
                                  @{@"title":@"重要通知",@"image":@"myicon04"},
                                  @{@"title":@"员工通讯录",@"image":@"myicon01"},
                                  @{@"title":@"CRM",@"image":@"myicon02"},
                                  @{@"title":@"我的项目",@"image":@"myicon03"}]];
        
        [arrayAllData addObject:@[@{@"title":@"推广效果",@"image":@"myicon04"},
                                  @{@"title":@"我的牌照",@"image":@"myicon05"},]];
        
        [arrayAllData addObject:@[@{@"title":@"合同模板",@"image":@"myicon06"},
                                  @{@"title":@"员工档案",@"image":@"myicon07"}]];
        
    }
//    [arrayAllData addObject:@[@{@"title":@"设置",@"image":@"myicon08"},]];
    
    [arrayAllData addObject:@[@{@"title":@"设置",@"image":@"myicon08"}]];
    [arrayAllData addObject:@[]];
}
-(void)refreshUserData{
//    XKFuser
    if (opT) {
        [opT cancel];
    }
    opT=[NetEngine createGetAction_LJ_two:[NSString stringWithFormat:XKFV3userinformation,[[Utility Share] userId],[[Utility Share] userToken]] onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            dicuserData=[resData objectForJSONKey:@"data"];
            _lblUserName.text=[dicuserData valueForJSONStrKey:@"realname"];
//            _lblUserSub.text=[dicuserData valueForJSONStrKey:@"department"];
            _lblUserSub.text=[NSString stringWithFormat:@"手机号:%@",[dicuserData valueForJSONStrKey:@"mobile"]];
            [_imageUserLogo imageWithURL:[dicuserData valueForJSONStrKey:@"path"] defaultImage:@"xkf_txmr"];
            [tableViewUser reloadData];
        }else{
            [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [super backButtonClicked:nil];
            });
        }
    } onError:^(NSError *error) {
        [SVProgressHUD showImage:nil status:alertErrorTxt];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [super backButtonClicked:nil];
        });
    }];
}

#pragma mark button
-(void)rightButtonClicked{
    if (dicuserData) {
//        [self pushController:[UpdateUserDataViewController class] withInfo:@"" withTitle:@"个人资料" withOther:dicuserData];
        [self pushController:[SetPersonalDataVC class] withInfo:@"" withTitle:@"个人资料" withOther:dicuserData];
        
    }else{
        [self refreshUserData];
    }
}
-(void)leftButtonCLicked{
    [self pushController:[UserResumeViewController class] withInfo:@"1" withTitle:@"我的简历" withOther:nil];
}
-(void)tapImageuserLogo{
    if (dicuserData) {
        NSMutableDictionary *dicCard=[[NSMutableDictionary alloc] initWithDictionary:dicuserData];
        [dicCard setValue:[dicuserData valueForJSONStrKey:@"card"] forKey:@"url"];
        [dicCard setValue:[[Utility Share] userId] forKey:@"uid"];
        [dicCard setValue:NSStringFromClass([self class]) forKey:@"controller"];
        [self pushController:[SelectWebUrlViewController class] withInfo:@"shareCard" withTitle:@" " withOther:dicCard];
    }else{
        [self refreshUserData];
    }
}
#pragma mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [arrayAllData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[arrayAllData objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[[arrayAllData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    static NSString *identifier =@"identifier_user";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.backgroundColor=[UIColor clearColor];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
        
        UIView *BGV=[[UIView alloc]initWithFrame:cell.bounds];
        [BGV setBackgroundColor:RGBCOLOR(255, 255, 255)];
        [cell setBackgroundView: BGV];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
       
        UIImageView *imageIcon=[RHMethods imageviewWithFrame:CGRectMake(0, 0, 36, 40) defaultimage:[dic valueForJSONStrKey:@"image"] contentMode:UIViewContentModeCenter];
        [cell addSubview:imageIcon];
        [imageIcon setTag:100];
        
        UILabel *lblName=[RHMethods labelWithFrame:CGRectMake(41, 13, 0, 20) font:Font(14) color:rgbTitleColor text:[dic valueForJSONStrKey:@"title"]];
        [cell addSubview:lblName];
        [lblName setTag:101];
        
        UIImageView *imageRedV=[RHMethods imageviewWithFrame:CGRectMake(XW(lblName)+2, 7, 10, 10) defaultimage:@"redTip070116" contentMode:UIViewContentModeCenter];
        imageRedV.tag=102;
        [cell addSubview:imageRedV];
        imageRedV.hidden=YES;
        [cell addSubview:[RHMethods imageviewWithFrame:CGRectMake(kScreenWidth-18, 13, 8, 15) defaultimage:@"righticon" contentMode:UIViewContentModeCenter]];
       NSArray*array= [arrayAllData objectAtIndex:indexPath.section];
        if (array.count-1!=indexPath.row) {
            
            [cell addSubview:[RHMethods lineViewWithFrame:CGRectMake(10, 39, kScreenWidth-10, 1)]];
        }
        
    }
    UIImageView *imageIcon=(UIImageView *)[cell viewWithTag:100];
    UIImageView *imageRedV=(UIImageView *)[cell viewWithTag:102];
    UILabel *lblName=(UILabel *)[cell viewWithTag:101];
    imageIcon.image=[UIImage imageNamed:[dic valueForJSONStrKey:@"image"]];
    lblName.text=[dic valueForJSONStrKey:@"title"];
    if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"我的项目"]) {
        imageRedV.hidden=![[dicuserData valueForJSONStrKey:@"unreadproject"] isEqualToString:@"1"];
    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"推广效果"]) {
        imageRedV.hidden=![[dicuserData valueForJSONStrKey:@"expand"] isEqualToString:@"1"];
    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"重要通知"]) {
        imageRedV.hidden=![[dicuserData valueForJSONStrKey:@"notice"] isEqualToString:@"1"];
    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"我的牌照"]) {
        imageRedV.hidden=![[dicuserData valueForJSONStrKey:@"myproducts"] isEqualToString:@"1"];
//    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"我的供求"]) {
//        imageRedV.hidden=![[dicuserData valueForJSONStrKey:@"mydemandtcom"] isEqualToString:@"1"];
//    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"重要通知"]) {
//        imageRedV.hidden=![[dicuserData valueForJSONStrKey:@"notice"] isEqualToString:@"1"];
    }else{
        imageRedV.hidden=YES;
    }
    
    float fw=[lblName sizeThatFits:CGSizeMake(MAXFLOAT, 20)].width;
    lblName.frameWidth=fw;
    imageRedV.frameX=XW(lblName)+2;
    
    return cell;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic=[[arrayAllData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    /*
     if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"我的阿米巴"]) {
     [self pushController:[UserFinanceViewController class] withInfo:@"" withTitle:[dic valueForJSONStrKey:@"title"] withOther:nil];
     }else
     */
    if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"我的项目"]) {
        [self pushController:[UserProjectViewController class] withInfo:@"" withTitle:[dic valueForJSONStrKey:@"title"] withOther:nil];
    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"推广效果"]) {
        [self pushController:[UserExtensionViewController class] withInfo:@"" withTitle:[dic valueForJSONStrKey:@"title"] withOther:nil];
    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"我的牌照"]) {
        [self pushController:[UserLicensePlateViewController class] withInfo:@"" withTitle:[dic valueForJSONStrKey:@"title"] withOther:nil];
//    }else if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"我的供求"]) {
//        [self pushController:[UserSupplyDemandViewController class] withInfo:@"" withTitle:[dic valueForJSONStrKey:@"title"] withOther:nil];
//    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"我的收藏"]){
//        [self pushController:[UserCollectionViewController class] withInfo:@"" withTitle:@"我的收藏" withOther:@{}];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"合同模板"]){
        [self pushDownloadView];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"员工通讯录"]){
        [self pushController:[UserColleagueViewController class] withInfo:@"" withTitle:[dic valueForJSONStrKey:@"title"] withOther:@{}];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"设置"]){
        [self pushController:[SetViewController class] withInfo:@"" withTitle:[dic valueForJSONStrKey:@"title"] withOther:@{}];
//    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"重要通知"]){
//        [self pushController:[CompanyNoticeViewController class] withInfo:@"" withTitle:[dic valueForJSONStrKey:@"title"] withOther:@{}];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"CRM"]){
//        [self pushController:[CRMViewController class] withInfo:@"" withTitle:@"CRM" withOther:nil];
        [self pushController:[NewCRMViewController class] withInfo:@"" withTitle:@"CRM" withOther:nil];
        
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"员工档案"]){
        [self pushController:[UserResumeViewController class] withInfo:@"" withTitle:@"简历" withOther:nil];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"重要通知"]){
        [self pushController:[CompanyNoticeViewController class] withInfo:@"" withTitle:@"重要通知" withOther:@{}];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"买家录入"]){
//        NSMutableDictionary*mdic=[NSMutableDictionary new];
//        [mdic setObject:@"46" forKey:@"id"];
//        
        [self pushController:[V4InputBuyerAndSeller class] withInfo:@"买家" withTitle:@"买家录入" withOther:nil];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"卖家录入"]){
//        NSMutableDictionary*mdic=[NSMutableDictionary new];
//        [mdic setObject:@"46" forKey:@"id"];
        
        [self pushController:[V4InputBuyerAndSeller class] withInfo:@"卖家" withTitle:@"卖家录入" withOther:nil];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"买家编辑"]){
        NSMutableDictionary*mdic=[NSMutableDictionary new];
        [mdic setObject:@"46" forKey:@"id"];
        
        [self pushController:[V4InputBuyerAndSeller class] withInfo:@"买家编辑" withTitle:@"买家编辑" withOther:mdic];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"买家详情"]){
        NSMutableDictionary*mdic=[NSMutableDictionary new];
        [mdic setObject:@"46" forKey:@"id"];
        
        [self pushController:[BuyerAndSellerDetailViewController class] withInfo:@"买家" withTitle:@"买家" withOther:mdic];
    }else if([[dic valueForJSONStrKey:@"title"] isEqualToString:@"录入"]){
       
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0.01;
    }
    return 10.f;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return nil;
    }
    MoreBlankTableViewHeaderFooterView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MoreBlankTableViewHeaderFooterView"];
    return sectionView;
}
-(void)pushDownloadView{
    //XKFV3productsdowncate
    if (arrayproductsdowncate) {
        NSString * title = @"";
        if ([[Utility Share] userIsADD]) {
            
            
            title = @"合同模板";
                
 
        }else{
            
            
            title = @"下载专区";
            
            
        }
         [self pushController:[UserDownloadViewController class] withInfo:@"" withTitle:title withOther:arrayproductsdowncate];
    }else{
        krequestParam
        [NetEngine createPostAction:XKFV3productsdowncate withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
            if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
                arrayproductsdowncate=[[resData objectForJSONKey:@"data"] objectForJSONKey:@"list"];
                [self pushDownloadView];
            }else{
                [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
            }
        }];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
